package com.robusta.cityuate.utils;

import com.robusta.cityuate.objects.User;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class Token {
	
	public static final String SHARED_PREFS_CONNECTION = "CITYUATE_SHARED";
	public static final String TOKEN = "token";
	
	/**
	 * saves the token gotten after login
	 * @param context
	 * @param token
	 * @return true if the token is saved
	 */
	public static boolean saveToken(Context context, String token,long userId){
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(TOKEN);
		editor.remove(User.USER_ID);
		editor.putString(TOKEN, token);
		editor.putLong(User.USER_ID, userId);
		editor.commit();

		return true;
	}
	
	/**
	 * removes the token from shared preferences
	 * @param context
	 * @return
	 */
	
	public static boolean removeToken(Context context){
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(TOKEN);
		editor.remove(User.USER_ID);
		editor.commit();
		return true;
	}
	
	/**
	 * fetches the token from the preferences
	 * @param context
	 * @return the token as string or null if not found
	 */
	public static String getToken(Context context){
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		return sharedPreferences.getString(TOKEN, null);
	}
	
	/**
	 * returns the current logged in User id
	 * @param context
	 * @return
	 */
	public static long getUserID(Context context){
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				SHARED_PREFS_CONNECTION, Context.MODE_PRIVATE);
		return sharedPreferences.getLong(User.USER_ID, 0);
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static User getUser(Activity context) {
		long id=getUserID(context);
		return new User(id,"first name","last name");
	}
	
	

}
